package gatech.cs2340.adventuretime.servlets;


	import java.io.*;
	import java.net.*;
	import java.util.*;

	public class PatientServer extends Thread {

	   private ServerSocket patientServer;
	 

	   public static void main(String argv[]) throws Exception {
	     new PatientServer();
	   }

	   public PatientServer() throws Exception {
	     patientServer = new ServerSocket(3000);
	     System.out.println("Server listening on port 3000.");
	     this.start();
	   } 

	   public void run() {
	     while(true) {
	       try {
	        System.out.println("Waiting for connections.");
	        Socket client = patientServer.accept();
	        System.out.println("Accepted a connection from: "+
	client.getInetAddress());
	        Connect c = new Connect(client);
	       } catch(Exception e) {}
	     }
	   }
	}

	class Connect extends Thread {
	   private Socket client = null;
	   private ObjectInputStream ois = null;
	   private ObjectOutputStream oos = null;
	    
	   public Connect() {}

	   public Connect(Socket clientSocket) {
	     client = clientSocket;
	     try {
	      ois = new ObjectInputStream(client.getInputStream());
	      oos = new ObjectOutputStream(client.getOutputStream());
	     } catch(Exception e1) {
	         try {
	            client.close();
	         }catch(Exception e) {
	           System.out.println(e.getMessage());
	         }
	         return;
	     }
	     this.start();
	   }

	  
	   public void run() {
	      try {
	         oos.writeObject(new Patient());
	         oos.flush();
	         // close streams and connections
	         ois.close();
	         oos.close();
	         client.close(); 
	      } catch(Exception e) {}       
	   }
	}


